package kevin.workplan.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kevin.workplan.database.gen.model.User;

public class SessionUtil {
	public static String SESSION_USER = "user";
	public static String SESSION_AUTHCODE = "authcode";
	
	/**
	 * 测试当前登陆用户是否是admin
	 * @param session
	 */
	public static boolean isLogin(HttpSession session) {
		User user = getUser(session);
		if (user == null)
			return false;
		else
			return true;
	}
	
	public static User getUser(HttpSession session) {
		return (User) session.getAttribute(SESSION_USER);
	}
	
	/**
	 * 验证码是否正确
	 * @param session
	 * @param code
	 * @return
	 */
	public static boolean validateCode(HttpSession session, String code) {
		String _code = (String) session.getAttribute(SESSION_AUTHCODE);
		session.removeAttribute(SESSION_AUTHCODE);		// 验证过后就删掉
		if(_code!=null && _code.equals(code))
			return true;
		return true;
	}
	
	public static void setPageParam(HttpServletRequest request,int total, int pagenum, int pagesize) {
		int totalpage = total / pagesize;
		if(total!=(totalpage*pagesize))
			totalpage++;
		request.setAttribute("totalpage", totalpage);
		request.setAttribute("pagesize", pagesize);
		request.setAttribute("pagenum", pagenum);
		boolean isLastPage = false;
		if(totalpage==pagenum)
			isLastPage = true;
		request.setAttribute("lastpage", isLastPage);
	}
}
